有了App介面和tflite model with metadata之後,
App的核心功能!靈魂!終於要被我實現了!
// Loading my custom model
val model = Effb0FerMeta.newInstance(this)
// Creates inputs for reference.
val tensorImage = TensorImage.fromBitmap(bitmap)
// Runs model inference and gets result.
val outputs = model.process(tensorImage)
.probabilityAsCategoryList.apply {
sortByDescending { it.score } // 排序,由高到低
}
val result = arrayListOf<String>()
for (output in outputs) {
val label = output.label
val score: Int = (output.score * 100).roundToInt()
result.add("表情是 $label 的可能性為 $score %")
}
val listView = findViewById<ListView>(R.id.listView)
listView.adapter = ArrayAdapter(this,
android.R.layout.simple_list_item_1,
result
)
try{
// ...
}catch(...){
// ...
}
private fun recognizeImage(bitmap: Bitmap) {
try {
// Loads my custom model
val model = Effb0FerMeta.newInstance(this)
// Creates inputs for reference.
val tensorImage = TensorImage.fromBitmap(bitmap)
// Runs model inference and gets result.
val outputs = model.process(tensorImage)
.probabilityAsCategoryList.apply {
sortByDescending { it.score } // 排序,由高到低
}
//取得辨識結果與可信度
val result = arrayListOf<String>()
for (output in outputs) {
val label = output.label
val score = (output.score * 100).roundToInt()
result.add("表情是 $label 的可能性為 $score %")
}
//將結果顯示於 ListView
val listView = findViewById<ListView>(R.id.listView)
listView.adapter = ArrayAdapter(this,
android.R.layout.simple_list_item_1,
result
)
} catch (e: IOException) {
e.printStackTrace()
}
}
這個程式碼區塊是我拖更很久的部分,
現在我補上來了。
基本上就是:
override fun onActivityResult(requestCode: Int,
resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 0 && resultCode == RESULT_OK) {
val image = data?.extras?.get("data") ?: return //取得資料
val bitmap = image as Bitmap
val imageView = findViewById<ImageView>(R.id.imageView)
imageView.setImageBitmap(bitmap) //使用 Bitmap 設定圖像
imageView.rotation = 90f
recognizeImage(bitmap)
}
if (requestCode == 1 && resultCode == RESULT_OK) {
val uri = data!!.data
val imageView = findViewById<ImageView>(R.id.imageView)
imageView.setImageURI(uri)
val drawable = imageView.drawable as BitmapDrawable
val bitmap = drawable.bitmap
recognizeImage(bitmap)
}
}
只要合併前幾天的文章內容,
應該已經有些帥氣/漂亮的觀眾們應該已經完成App了。
這裡有完整的code,不嫌棄的話給個star吧 :D
明天就是最後一天了,
內容應該是成果發表和感言吧XD